各位大神好:
在網路看過如下的程式碼會報錯
const testPost = (req, res) => {
const insertValues = req.body;
... 略
connection.query('INSERT INTO table_1 SET ?', insertValues, (error, result) => {
此時建議可改用如下的程式碼
const testPost = (req, res) => {
var jsonData = req.body;
var insertValues = [];
insertValues.push (jsonData[0].user_id, jsonData[0].article_title,
jsonData[0].article_tag, jsonData[0].article_content);
... 略
connection.query ('INSERT INTO Article (user_id, article_title,
article_tag, article_content) VALUES (' + insertValues[0] + ','
+ '\'' + insertValues[1] + '\',' + '\'' + insertValues[2] +
+ '\',' + '\'' + insertValues[3] + '\'' + ');', (error, result) => {
以下是 JSON 格式
[
{
"user_id": 1,
"article_title": "Node.js",
"article_tag": "Backend",
"article_content": "Welcome to Node.js demo"
}
]